//
//  EditProductView.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2021/9/15.
//  Copyright © 2021 zwb. All rights reserved.
//

import UIKit

class ZyaEditProductView: UIView {
    var confirmClosure:(()->())?
    var deleteClosure:(()->())?
    lazy var bg:UIView = {
        let v = UIView()
        v.backgroundColor = .black
        v.alpha = 0.3
        return v
    }()
    lazy var contentV:UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 5
        return v
    }()
    lazy var closeBtn:UIButton = {
        let v = UIButton()
        v.setBackgroundImage(UIImage(named:"delete"), for: .normal)
        return v
    }()
    lazy var titleLab:UILabel = {
        let v = UILabel()
        return v
    }()
    
    var item1:KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(32)
        let v = KSFormHItem(title: "规格",appearance: appe)
        return v
    }()
    var item2 :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(32)
        let v = KSFormHItem(title: "价格",appearance: appe)
        return v
    }()
    var item3 :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(32)
        let v = KSFormHItem(title: "主辅",appearance: appe)
        return v
    }()
    
    var item4 :KSFormHItem = {
        let appe = KSFormHAppear()
            .threeDemiInput(true)
            .tailDesc("KG")
        let v = KSFormHItem(title: "重量",appearance: appe)
        return v
    }()
    var item5 :KSFormHItem = {
        let appe = KSFormHAppear()
            .threeDemiInput(true)
            .tailDesc("PCS")
            .contentTrailing(32)
        let v = KSFormHItem(title: "数量",appearance: appe)
        return v
    }()
    var item6 :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(32)
        let v = KSFormHItem(title: "金额",appearance: appe, type: .tempTextView)
        return v
    }()
    var item7 :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(32)
        let v = KSFormHItem(title: "备注",appearance: appe)
        return v
    }()
    lazy var confirmBtn:UIButton = {
        let v = UIButton()
        v.setTitleColor(.white, for: .normal)
        v.setTitle("确定", for: .normal)
        v.backgroundColor = Theme.primaryColor()
        v.layer.cornerRadius = 20
        return v
    }()
    override init(frame:CGRect) {
        super.init(frame: frame)
        bg.frame = self.bounds
        self.addSubview(bg)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction))
        bg.addGestureRecognizer(tap)
        
        self.addSubview(contentV)
        contentV.snp.makeConstraints { make in
            make.left.equalTo(bg).offset(10)
            make.right.equalTo(bg).offset(-10)
            make.centerY.equalTo(bg)
        }
        contentV.addSubview(closeBtn)
        closeBtn.addTarget(self, action: #selector(deleteAction), for: .touchUpInside)
        closeBtn.snp.makeConstraints { make in
            make.top.equalTo(contentV).offset(8)
            make.right.equalTo(contentV).offset(-8)
            make.width.equalTo(30)
            make.height.equalTo(30)
        }
        contentV.addSubview(titleLab)
        titleLab.snp.makeConstraints { make in
            make.top.equalTo(contentV).offset(10)
            make.centerX.equalTo(contentV)
        }
        
        contentV.addSubview(item1)
        item1.actionClosure = {}
        item1.snp.makeConstraints { make in
            make.top.equalTo(titleLab.snp.bottom).offset(10)
            make.left.right.equalTo(contentV)
            make.height.equalTo(43)
        }
        contentV.addSubview(item2)
        item2.actionClosure = {}

        item2.snp.makeConstraints { make in
            make.left.right.equalTo(contentV)
            make.top.equalTo(item1.snp.bottom)
            make.height.equalTo(43)
        }
        contentV.addSubview(item3)
        item3.actionClosure = {}

        item3.snp.makeConstraints { make in
            make.left.right.equalTo(contentV)
            make.top.equalTo(item2.snp.bottom)
            make.height.equalTo(43)
        }
        contentV.addSubview(item4)
        item4.snp.makeConstraints { make in
            make.left.right.equalTo(contentV)
            make.top.equalTo(item3.snp.bottom)
            make.height.equalTo(43)
        }
        contentV.addSubview(item5)
        item5.snp.makeConstraints { make in
            make.left.right.equalTo(contentV)
            make.top.equalTo(item4.snp.bottom)
            make.height.equalTo(43)
         }
        contentV.addSubview(item6)
        item6.actionClosure = {}

        item6.snp.makeConstraints { make in
            make.left.right.equalTo(contentV)
            make.top.equalTo(item5.snp.bottom)
            make.height.equalTo(43)
         }
        contentV.addSubview(item7)
        item7.snp.makeConstraints { make in
            make.left.right.equalTo(contentV)
            make.top.equalTo(item6.snp.bottom)
            make.height.equalTo(43)
         }
        contentV.addSubview(confirmBtn)
        confirmBtn.addTarget(self, action: #selector(confirmAction), for: .touchUpInside)
        confirmBtn.snp.makeConstraints { make in
            make.width.equalTo(130)
            make.top.equalTo(item7.snp.bottom).offset(15)
            make.centerX.equalTo(contentV)
            make.height.equalTo(40)
            make.bottom.equalTo(contentV).offset(-15)
        }
        item4.textValueChangeClo = {str in
            self.countNum()
            self.countSumMoney()
        }
        item5.textValueChangeClo = {str in
            self.countWeight()
            self.countSumMoney()
        }
     

     }
 
    func countNum() {
        let zR = item3.textView.text.unwrap.preChar(char: ":")
        let fR = item3.textView.text.unwrap.sufChar(char: ":")
        let num = doubleTran(item4.textView.text.unwrap) * (doubleTran(fR) / doubleTran(zR))
        item5.setVal(num.threeZero)
        
    }
    func countWeight() {
        let zR = item3.textView.text.unwrap.preChar(char: ":")
        let fR = item3.textView.text.unwrap.sufChar(char: ":")
        let weight = doubleTran(item5.textView.text.unwrap) * (doubleTran(zR) / doubleTran(fR))
        item4.setVal(weight.threeZero)
    }
    
    
    
    
    
    func countSumMoney() {
        let price = item2.textView.text.unwrap.replacingOccurrences(of: "元/Kg", with: "")
        let sumMoney = (doubleTran(price) * doubleTran(item4.textView.text.unwrap)).roundTo(2)
        item6.setVal(sumMoney.twoZero + "元")
    }
    @objc func confirmAction() {
        if let clo = self.confirmClosure {
            clo()
        }
    }
    @objc func deleteAction() {
        if let clo = self.deleteClosure {
            clo()
        }
    }
    @objc func tapAction() {
        self.removeFromSuperview()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
